<!DOCTYPE html>
<html>
<head>
	<title>拓攻中奖名单</title>
</head>
<body ng-app="prize">
	<table ng-controller="PrizeCtrl" border="1" style="margin:100px auto;">
		<caption style="margin-bottom:24px;">拓攻中奖名单</caption>
		<thead>
			<tr>
				<td>序号</td>
				<td>微信昵称</td>
				<td>微信openid</td>
				<td>微信头像</td>
				<td>奖项级别</td>
				<td>奖项名称</td>
				<td>使用状态</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody ng-if="prizes && prizes.length">
			<tr ng-repeat="prize in prizes track by $index">
				<td>{{$index+1}}</td>
				<td>{{prize.user_id.nickname}}</td>
				<td>{{prize.user_id.openid}}</td>
				<td><img src="{{prize.user_id.headimgurl}}" style="width:20px;height:20px;"/></td>
				<td>{{prize.code}}</td>
				<td>{{prize.desp}}</td>
				<td>{{prize.use_state==1?'未使用':'已使用'}}</td>
				<td ng-if="prize.use_state==1"><button ng-click="use(prize._id)">使用</button></td>
			</tr>
		</tbody>
	</table>
</body>
<script src="/static/bower_components/angular/angular.min.js"></script>
<script type="text/javascript">
	var prize = angular.module('prize',[])
		.controller('PrizeCtrl',function($scope,PrizeService){
			var promise = new PrizeService.queryAsyncAll();
			promise.then(function(data){
				$scope.prizes = data;
			},function(data){
				console.log(data)
			})

			$scope.use = function(prize_id){
				console.log(prize_id)
				var promise = new PrizeService.ensureUse({prize_id:prize_id});
				promise.then(function(data){
					if(data.code){
						alert('使用成功')
					}else{
						console.log('有错误')
					}
					window.location.href="http://wechat.topxgun.com/prize/queryAll"
				},function(data){
					console.log('有错误')
				})
			}
		})
		.service('PrizeService',['$http','$q',function($http,$q){
			var queryAsyncAll = function(data){
				var deferred = $q.defer();
				$http({method:'GET',url:'http://wechat.topxgun.com/prize/queryAsyncAll'})
				.success(function(data,status,headers,config){
					deferred.resolve(data);
				})
				.error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}

			var ensureUse = function(data){
				var deferred = $q.defer();
				$http({method:'POST',url:'http://wechat.topxgun.com/prize/ensureUse',data:data})
				.success(function(data,status,headers,config){
					deferred.resolve(data);
				})
				.error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}

			return {
				queryAsyncAll:queryAsyncAll,
				ensureUse:ensureUse
			}

		}])
</script>
</html>